package com.etl.es.controller;

import com.etl.es.charts.base.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Collections;

@Slf4j
@RestController
@RequestMapping("/charts")
public class ChartController {

    @Autowired
    CmsSearchChart cmsSearchChart;

    @PostMapping("/channel-order")
    public ChartResult getChannelOrder(@Validated @RequestBody ChartParamOrder param) {
        param.setChannelTypeCode("1");
        param.setGroupField("channelName");
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_ORDER_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_ORDER));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/channel-type-order")
    public ChartResult getChannelTypeOrder(@Validated @RequestBody ChartParamOrder param) {
        param.setGroupField("channelTypeName");
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_ORDER_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_ORDER));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/cinema-order")
    public ChartResult getCinemaOrder(@Validated @RequestBody ChartParamOrder param) {
        param.setGroupField("cinemaName");
        param.setMaxSize(8);
        param.setChartColumns(Arrays.asList("影院", "交易金额"));
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_ORDER_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_ORDER));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/film-ticket")
    public ChartResult getFilmTicket(@Validated @RequestBody ChartParamFilmTicket param) {
        param.setGroupField("filmName");
        param.setMaxSize(8);
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_GOODS_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_FILM_TICKET));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/tenant-order")
    public ChartResult getTenantOrder(@Validated @RequestBody ChartParamOrder param) {
        param.setGroupField("tenantName");
        param.setChartColumns(Arrays.asList("院线", "交易金额"));
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_ORDER_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_ORDER));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/time-order")
    public ChartResult getTimeOrder(@Validated @RequestBody ChartParamOrder param) {
        param.setGroupField("orderHour");
        param.setOrderField("orderHour");
        param.setOrderAsc(true);
        param.setMaxSize(24);
        param.setChartColumns(Arrays.asList("时间", "交易金额"));
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_ORDER_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_ORDER));
        return cmsSearchChart.search(param);
    }

    @PostMapping("/type-goods")
    public ChartResult getTypeGoods(@Validated @RequestBody ChartParamGoods param) {
        param.setGroupField("goodsTypeName");
        param.setAggregationField(AggregationCondition.AGGREGATION_FIELD_GOODS_PRICE);
        param.setSearchIndices(Collections.singletonList(ESConst.ES_INDEX_CMS_GOODS));
        return cmsSearchChart.search(param);
    }
}
